﻿using System;
using System.Collections.Generic;
using System.DrawingCore;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShortcutMenu
{
    public static class GenerateMatrixOrder
    {
        public static Point GetPoint(ref int[][] arr, int x, int y, int count)
        {
            if (count > arr[0].Length)
            {
                return new Point(-1, -1);
            }
            if (x >= arr[0].Length)
            {
                return new Point(-1, -1);
            }
            if (y >= arr.Length)
            {
                return new Point(-1, -1);
            }
            var time = DateTime.Now;
            //while (time.AddSeconds(5) >= DateTime.Now)
            while (true)
            {
                if (arr[y][x] == 0)
                {
                    //判断当前空间是否足够
                    var judge = true;
                    //当前行的余位
                    if (x + count > arr[0].Length)
                    {
                        judge = false;
                    }
                    //当前列的余位
                    if (y + count > arr.Length)
                    {
                        //添加行
                        //查询缺少几行
                        var num = y + 1 + count - arr.Length;
                        //添加高度
                        var newArr = new int[arr.Length + num][];
                        //初始化新行
                        for (int i = arr.Length - 1; i < newArr.Length; i++)
                        {
                            newArr[i] = new int[arr[0].Length];
                        }
                        for (int i = 0; i < arr.Length; i++)
                        {
                            newArr[i] = arr[i];
                        }
                        arr = newArr;
                    }
                    for (int i = x; i < x + count && judge; i++)
                    {
                        for (int j = y; j < y + count; j++)
                        {
                            if (arr[j][i] != 0)
                            {
                                judge = false;
                            }
                        }
                    }
                    if (judge)
                    {
                        return new Point(x, y);
                    }
                }

                //这个地方有人了
                x++;
                if (x >= arr[0].Length)
                {
                    x = 0;
                    y++;

                    if (y >= arr.Length)
                    {
                        //加一行
                        var newArr = new int[arr.Length + 1][];
                        newArr[arr.Length] = new int[arr[0].Length];
                        for (int i = 0; i < arr.Length; i++)
                        {
                            newArr[i] = arr[i];
                        }
                        arr = newArr;
                    }


                }
            }
            Log.Write("GenerateMatrixOrder运算超时!");
            return new Point(-1, -1);
        }

        static int ItemNum = 2;
        public static void Set(ref int[][] arr, Point point, int count)
        {
            var X = point.X;
            var Y = point.Y;
            for (int i = 0; i < count * count; i++)
            {
                arr[Y][X] = ItemNum;

                X++;
                if (X >= count + point.X)
                {
                    X = point.X;
                    Y++;
                }
            }
            ItemNum++;
        }

        public static void For(int[][] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                for (int j = 0; j < arr[0].Length; j++)
                {
                    Console.Write($"{arr[i][j]}    ");
                }
                Console.WriteLine();
            }
        }
    }
}
